#include "conversion.h"

using namespace Utils;

QRect Conversion::variantToRect(const QVariant& v)
{
    QVariantList vmap = v.toList();
    QPoint p = variantToPoint(vmap.takeFirst());
    QSize s = variantToSize(vmap.takeFirst());
    return QRect(p, s);
//    int x = vmap.value("x").toInt();
//    int y = vmap.value("y").toInt();
//    int w = vmap.value("w").toInt();
//    int h = vmap.value("h").toInt();
//    return QRect(x, y, w, h);
}

QRectF Conversion::variantToRectF(const QVariant& v)
{
    QVariantList vmap = v.toList();
    QPointF p = variantToPointF(vmap.takeFirst());
    QSizeF s = variantToSizeF(vmap.takeFirst());
    return QRectF(p, s);
//    qreal x = vmap.value("x").toFloat();
//    qreal y = vmap.value("y").toFloat();
//    qreal w = vmap.value("w").toFloat();
//    qreal h = vmap.value("h").toFloat();
//    return QRectF(x, y, w, h);
}

QVariant Conversion::rectToVariant(const QRect& rect)
{
    QVariantList vrect;
    QVariant point = pointToVariant(rect.topLeft());
    QVariant size = sizeToVariant(rect.size());

   vrect  << point << size;
//    vrect.insert("x", rect.x());
//    vrect.insert("y", rect.y());
//    vrect.insert("w", rect.width());
//    vrect.insert("h", rect.height());
    return vrect;
}

QVariant Conversion::rectFToVariant(const QRectF& rect)
{
    QVariantList vrect;
    QVariant point = pointFToVariant(rect.topLeft());
    QVariant size = sizeFToVariant(rect.size());
    vrect << point << size;
//    vrect.insert("x", rect.x());
//    vrect.insert("y", rect.y());
//    vrect.insert("w", rect.width());
//    vrect.insert("h", rect.height());
    return vrect;
}

QPoint Conversion::variantToPoint(const QVariant& v)
{
    QVariantMap vmap = v.toMap();
    int x = vmap.value("x").toInt();
    int y = vmap.value("y").toInt();
    return QPoint(x, y);
}

QPointF Conversion::variantToPointF(const QVariant& v)
{
    QVariantMap vmap = v.toMap();
    qreal x = vmap.value("x").toFloat();
    qreal y = vmap.value("y").toFloat();
    return QPointF(x, y);
}

QVariant Conversion::pointFToVariant(const QPointF& point)
{
    QVariantMap vrect;
    vrect.insert("x", point.x());
    vrect.insert("y", point.y());
    return vrect;
}

QVariant Conversion::pointToVariant(const QPoint& point)
{
    QVariantMap vrect;
    vrect.insert("x", point.x());
    vrect.insert("y", point.y());
    return vrect;
}

QSize Conversion::variantToSize(const QVariant& v)
{
    QVariantMap vmap = v.toMap();
    int w = vmap.value("w").toInt();
    int h = vmap.value("h").toInt();
    return QSize(w, h);
}

QSizeF Conversion::variantToSizeF(const QVariant& v)
{
    QVariantMap vmap = v.toMap();
    qreal w = vmap.value("w").toFloat();
    qreal h = vmap.value("h").toFloat();
    return QSizeF(w, h);
}

QVariant Conversion::sizeFToVariant(const QSizeF& size)
{
    QVariantMap vrect;
    vrect.insert("w", size.width());
    vrect.insert("h", size.height());
    return vrect;
}

QVariant Conversion::sizeToVariant(const QSize& size)
{
    QVariantMap vrect;
    vrect.insert("w", size.width());
    vrect.insert("h", size.height());
    return vrect;
}
